package com.jgx.dao;
import com.jgx.bean.Breakfast503;
import com.jgx.util.Dbutil509;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BreakfastDao503{
    public List<Breakfast503> findAll() {
        Connection connection = Dbutil509.getConnection();
        if (connection == null) {
            System.out.println("数据库连接失败");
            return new ArrayList<>(); // 返回空列表，表示连接失败
        }
        List<Breakfast503> bList = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from breakfastreservation";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Breakfast503 breakfast = new Breakfast503();
                breakfast.setBid(resultSet.getInt(1));
                breakfast.setBcid(resultSet.getInt(2));
                breakfast.setbIDcard(resultSet.getString(3));
                breakfast.setBreakfastType(resultSet.getString(4));
                breakfast.setbPayPrice(resultSet.getString(5));
                breakfast.setBtime(resultSet.getString(6));
                bList.add(breakfast);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Dbutil509.closeAll(resultSet, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for(Breakfast503 breakfast : bList) {
            System.out.println(breakfast.toString());
        }
        return bList;
    }

    public void deleteByid(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "delete from breakfastreservation where ReservationID = ?";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                Dbutil509.closeAll(null, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void add(Breakfast503 breakfast)  {
        Connection connection = null;
        PreparedStatement statement =null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "INSERT INTO breakfastreservation(ReservationID,CustomerID,IDCardNumber,BreakfastType,PayPrice,BreakfastReservationTime) VALUES(?,?,?,?,?,?)";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,breakfast.getBid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(2, breakfast.getBcid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,breakfast.getbIDcard());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(4, breakfast.getBreakfastType());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(5, breakfast.getbPayPrice());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(6, breakfast.getBtime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void update(Breakfast503 breakfast){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "UPDATE breakfastreservation SET CustomerID=?,IDCardNumber=?,BreakfastType=?,PayPrice=?,BreakfastReservationTime=? where ReservationID=?";
        try {
            statement=connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(6,breakfast.getBid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,breakfast.getBcid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2,breakfast.getbIDcard());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,breakfast.getBreakfastType());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(4,breakfast.getbPayPrice());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(5,breakfast.getBtime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

